package org.bonorsoft.modules.control.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.modules.wechat.entity.WxRequest;
import org.bonorsoft.modules.wechat.entity.WxResponse;
import org.bonorsoft.modules.wechat.service.IWxRequestService;
import org.bonorsoft.modules.wechat.service.IWxResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("/control/response")
public class ResponseController  extends BonorController<WxResponse, IWxResponseService> {
    private String prefix = "control/response";

    @Autowired
    private IWxResponseService wxResponseService;

    @Autowired
    private IWxRequestService wxRequestService;

    /**
     * 呼应 回复 添加页面
     */
    @GetMapping("/add/{qqid}")
    public String add(@PathVariable("qqid") String qqid, ModelMap map) {
        WxRequest wxRequest = wxRequestService.getById(qqid);
        map.put("wxRequest",wxRequest);
        return prefix + "/add";
    }

    /**
     * 呼应 回复 添加
     * @param wxResponse
     * @return
     */
    @AutoLog(value = "呼应-回复-添加")
    @RequiresPermissions("control:response:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated WxResponse wxResponse){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String hfid = UUID.randomUUID().toString().replaceAll("-", "");
        wxResponse.setHfid(hfid).setHfrid(user.getUsername()).setHfrxm(user.getRealname()).setHfsj(new Date());
        wxResponseService.save(wxResponse);
        return Result.ok("添加成功！");
    }
}
